// const BASE_URL = 'http://127.0.0.1:9750'
// const BASE_URL = 'http://192.168.0.103:9750'
const BASE_URL = 'http://cp.wangrunlin.com:9750'

const request = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + options.url,
			method: options.method,
			data: options.data,
			header: {
				'content-type': 'application/x-www-form-urlencoded',
			},
			success: (res) => {
				if (res.statusCode !== 200) {
					return uni.showToast({
						title: "获取数据失败"
					});
				}
				resolve(res);
			},
			fail: (err) => {
				uni.showToast({
					title: "接口请求失败，请联系管理员！",
					icon: 'none'
				});
				reject(err);
			}
		})
	});
}

function formatDate(date, fmt) {
	date = new Date(date);
	fmt = "yyyy-MM-dd";
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
}

export {
	request,
	formatDate
}